package com.linrui.org.service.impl;

import com.linrui.basic.common.MailCommonService;
import com.linrui.basic.domain.BaseConstants;
import com.linrui.basic.service.IBaiduAiService;
import com.linrui.basic.utils.AjaxResult;
import com.linrui.org.domain.Employee;
import com.linrui.org.domain.Shop;
import com.linrui.org.domain.ShopAuditLog;
import com.linrui.org.mapper.EmployeeMapper;
import com.linrui.org.mapper.ShopAuditLogMapper;
import com.linrui.org.mapper.ShopMapper;
import com.linrui.org.service.IShopService;
import com.linrui.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author linrui
 * @since 2022-11-01
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper mapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IBaiduAiService baiduAiService;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    @Autowired
    private MailCommonService mailCommonService;
    @Override
    public void settlement(Shop shop) {
        Shop shop1 = mapper.getByName(shop.getName());
        System.out.println(shop1);
        //校验
        validate(shop, shop1);

        // 2 保存店铺信息 - 店铺状态:待审核 注册时间:当前时间
        // 注意:如果店铺状态=驳回,那么它就不算已存在,而应该直接将状态修改为=待审核
        if (!Objects.isNull(shop1) && shop1.getState().equals(BaseConstants.Shop.STATE_REJECT_AUDIT)){
            shop.setId(shop1.getId());
        }
        shop.setState(BaseConstants.Shop.STATE_WAIT_AUTID);//设置店铺状态=待审核
        shop.setRegisterTime(new Date());
        //之前数据库已经存在这个店铺了
        if (shop.getId()!=null){
            mapper.update(shop);
        }else{
            mapper.insert(shop);
        }
        // 3 保存店铺管理员信息 思考:如果店铺管理员已经存在怎么办?
        Employee admin = shop.getAdmin();//填入的管理员
        //查询管理员是否存在
        Employee admin1 = employeeMapper.getByName(admin.getUsername());
        if (Objects.nonNull(admin1)){
            admin.setId(admin1.getId());//如果店铺管理员已经存在,就使用原来已经存在的管理员信息
        }else {
            admin.setState(BaseConstants.Employee.STATE_WAIT_AUTID);
            employeeMapper.insert(admin);
            shop.setAdminId(admin.getId());
        }
        // 4 调用自动审核审核店铺的名字和地址是否合规
        String text = shop.getName()+""+shop.getAddress();
        AjaxResult ajaxResult = baiduAiService.audit(text, null, null);
        // 4.1 如果合规,那么店铺的状态=待激活 并且发送一个邮件给店铺管理员
        if (ajaxResult.getSuccess()){
            shop.setState(BaseConstants.Shop.STATE_WAIT_ACTIVE);
        }else {
            // 4.2 如果不合规,店铺的状态=驳回 如果驳回可以给店铺发送个邮件或者短信,告知申请被驳回了和驳回原因
            shop.setState(BaseConstants.Shop.STATE_REJECT_AUDIT);
        }
        // 保存店铺的管理员
        mapper.update(shop);
        // 5 要记录店铺审核日志到t_org_shop_aduit_log表中
        ShopAuditLog log = new ShopAuditLog();
        log.setState(shop.getState());
        log.setShopId(shop.getId());
        log.setAuditTime(new Date());
        log.setNote(ajaxResult.getSuccess()?"审核通过":"审核驳回"+ajaxResult.getMessage());
        shopAuditLogMapper.insert(log);
        try {
            mailCommonService.calling();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public void active(Long id) {
        mapper.loadById(id).setState(BaseConstants.Shop.STATE_ACTIVE_SUCCESS);
    }


    private void validate(Shop shop, Shop shop1) {
        //        1.1.判断传入的对象/填写的店名/填写的管理员名
//        判断填写的管理员各项信息是否为空
        if (Objects.isNull(shop)){throw new RuntimeException("请填写相应信息");}
        if (StringUtils.isEmpty(shop.getName())){throw new RuntimeException("请填写相应信息");}
        if (StringUtils.isEmpty(shop.getAdmin())){throw new RuntimeException("请填写相应信息");}
        Employee admin = shop.getAdmin();
        if (StringUtils.isEmpty(admin.getUsername())
                || StringUtils.isEmpty(admin.getPassword())
                || StringUtils.isEmpty(admin.getEmail())
                || StringUtils.isEmpty(admin.getComfirmPassword())
                || StringUtils.isEmpty(admin.getPhone())){
            throw new RuntimeException("请填写相应信息");
        }
//        1.2.检验店铺是否存在
//        存在就退回
//         不存在就添加
//        店铺状态为驳回的话,就改为待审核
        if (!Objects.isNull(shop1)&&!shop1.equals(BaseConstants.Shop.STATE_REJECT_AUDIT)){
            throw new RuntimeException("店铺已存在，请勿重新入驻");
        }
//        1.3.比对两次密码是否相等
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new RuntimeException("两次密码输入不一致，请重新输入");
        }
    }
}
